package com.jcl.recruit.resume.service;

import com.jcl.recruit.core.constants.ResumeLogType;
import com.jcl.core.web.utils.RequestUtils;
import com.jcl.recruit.log.logic.LoggerProxy;
import com.jcl.recruit.resume.logic.ResumeBasicInfoLogic;
import com.jcl.recruit.resume.logic.ResumeTransferLogic;
import com.jcl.recruit.resume.model.ResumeBasicInfo;
import com.jcl.recruit.user.logic.UserResumeAttentionLogic;
import com.jcl.recruit.user.model.UserResumeAttention;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ResumeAttentionService {

    @Autowired
    private UserResumeAttentionLogic userResumeAttentionLogic;

    @Autowired
    private ResumeBasicInfoLogic resumeBasicInfoLogic;

    @Autowired
    private ResumeTransferLogic resumeTransferLogic;

    @Autowired
    private LoggerProxy loggerProxy;

    public void attentionResume(Integer userId, Integer secResumeId, Integer fromType) {

        String corpCode = RequestUtils.getCorpCode();
        List<UserResumeAttention> attentionList = null;
        if (0 == fromType) {
            attentionList = userResumeAttentionLogic.getListBySearchResumeId(userId, secResumeId);
        } else {
            attentionList = userResumeAttentionLogic.getListByResumeId(userId, secResumeId);
        }
        if (CollectionUtils.isNotEmpty(attentionList)) {
            UserResumeAttention attention = attentionList.get(0);
            userResumeAttentionLogic.update(attention.getId(), 1 == attention.getStatus() ? 0 : 1);
        } else {
            ResumeBasicInfo resume = null;
            if (0 == fromType) {
                resume = resumeBasicInfoLogic.findOneByExternalKey(corpCode, secResumeId.toString());
            } else {
                resume = resumeBasicInfoLogic.findOne(secResumeId);
            }
            if (null == resume) {
                try {
                    resume = resumeTransferLogic.transferResumeById(secResumeId,
                            ResumeLogType.TYPE_ADD_ATTENTION, corpCode);
                    // 添加日志
                    loggerProxy.addResumeLog(resume.getId(), ResumeLogType.TYPE_ADD_ATTENTION);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            UserResumeAttention attention = new UserResumeAttention();
            attention.setUserId(userId);
            attention.setResumeId(resume.getId());
            attention.setSearchResumeId(secResumeId);
            attention.setApplyOtherJid(resume.getApplyOtherJid());
            attention.setStatus(1);
            attention.setCreateTime(new Date());
            attention.setUpdateTime(new Date());
            userResumeAttentionLogic.add(attention);
        }
    }

    public List<UserResumeAttention> getListBySearchResumeIds(Integer userId, List<Integer> searchResumeIds) {
        return userResumeAttentionLogic.getListBySearchResumeIds(userId, searchResumeIds);
    }

    public List<UserResumeAttention> getListBySearchResumeId(Integer userId, Integer searchResumeId) {
        return userResumeAttentionLogic.getListBySearchResumeId(userId, searchResumeId);
    }

    public List<UserResumeAttention> getListByResumeId(Integer userId, Integer resumeId) {
        return userResumeAttentionLogic.getListByResumeId(userId, resumeId);
    }

    public List<UserResumeAttention> getListByUserId(Integer userId) {
        return userResumeAttentionLogic.getListByUserId(userId);
    }

}
